package cn.iocoder.yudao.module.scenic.service.comment;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.scenic.controller.admin.checkin.vo.CheckInPageByLocationResVO;
import cn.iocoder.yudao.module.scenic.controller.admin.comment.vo.CommentPageByLocationReqVO;
import cn.iocoder.yudao.module.scenic.controller.admin.comment.vo.CommentPageByLocationResVO;
import cn.iocoder.yudao.module.scenic.controller.admin.comment.vo.CommentPageReqVO;
import cn.iocoder.yudao.module.scenic.controller.admin.comment.vo.CommentSaveReqVO;
import cn.iocoder.yudao.module.scenic.dal.dataobject.comment.CommentDO;
import cn.iocoder.yudao.module.scenic.dal.mysql.comment.CommentMapper;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.scenic.enums.ErrorCodeConstants.COMMENT_NOT_EXISTS;

/**
 * 实践点评论 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CommentServiceImpl implements CommentService {

    @Resource
    private CommentMapper commentMapper;

    @Resource
    DeptApi deptApi;

    @Override
    public Long createComment(CommentSaveReqVO createReqVO) {
        // 插入
        CommentDO comment = BeanUtils.toBean(createReqVO, CommentDO.class);
        comment.setCommentStatus(1);
        commentMapper.insert(comment);
        // 返回
        return comment.getId();
    }

    @Override
    public void updateComment(CommentSaveReqVO updateReqVO) {
        // 校验存在
        validateCommentExists(updateReqVO.getId());
        // 更新
        CommentDO updateObj = BeanUtils.toBean(updateReqVO, CommentDO.class);
        commentMapper.updateById(updateObj);
    }

    @Override
    public void deleteComment(Long id) {
        // 校验存在
        validateCommentExists(id);
        // 删除
        commentMapper.deleteById(id);
    }

    private void validateCommentExists(Long id) {
        if (commentMapper.selectById(id) == null) {
            throw exception(COMMENT_NOT_EXISTS);
        }
    }

    @Override
    public CommentDO getComment(Long id) {
        return commentMapper.selectById(id);
    }

    @Override
    public PageResult<CommentDO> getCommentPage(CommentPageReqVO pageReqVO) {
        pageReqVO.setCommentStatus(2);
        return commentMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<CommentPageByLocationResVO> commentPageByLocation(CommentPageByLocationReqVO pageReqVO) {
        IPage<CommentPageByLocationResVO> allPage = new Page(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        IPage<CommentPageByLocationResVO> commentDOPageResult = commentMapper.selectCommentListByCondition(allPage,pageReqVO);
        PageResult<CommentPageByLocationResVO> page = new PageResult<>();
        if( 0L != commentDOPageResult.getTotal()) {
            Set<Long> idSet = new HashSet<>();
            for (CommentPageByLocationResVO spotDO : commentDOPageResult.getRecords()) {
                idSet.add(spotDO.getProvinceId());
                idSet.add(spotDO.getCityId());
                idSet.add(spotDO.getDistrictId());
            }
            List<Long> idList = new ArrayList<>(idSet);
            CommonResult<List<DeptRespDTO>> result = deptApi.getDeptList(idList);
            Map<Long, DeptRespDTO> deptRespDTOMap = new HashMap<>();
            if (result.isSuccess()) {
                List<DeptRespDTO> deptRespDTOList = result.getData();
                if (!CollectionUtils.isEmpty(deptRespDTOList)) {
                    deptRespDTOMap = deptRespDTOList.stream().collect(Collectors.toMap(DeptRespDTO::getId, Function.identity()));
                }
            }
            Map<Long, DeptRespDTO> finalDeptRespDTOMap = deptRespDTOMap;
            List<CommentPageByLocationResVO> spotRespVOList = commentDOPageResult.getRecords().stream().map(s -> {
                Long provinceId = s.getProvinceId();
                Long cityId = s.getCityId();
                Long districtId = s.getDistrictId();
                StringBuilder belongAddress = new StringBuilder();
                if (finalDeptRespDTOMap.containsKey(provinceId)) {
                    belongAddress.append(finalDeptRespDTOMap.get(provinceId).getName());
                    belongAddress.append("-");
                }
                if (finalDeptRespDTOMap.containsKey(cityId)) {
                    belongAddress.append(finalDeptRespDTOMap.get(cityId).getName());
                    belongAddress.append("-");
                }
                if (finalDeptRespDTOMap.containsKey(districtId)) {
                    belongAddress.append(finalDeptRespDTOMap.get(districtId).getName());
                }
                s.setBelongAddress(belongAddress.toString());
                return s;
            }).collect(Collectors.toList());
            page.setList(spotRespVOList);
        }
        page.setTotal(commentDOPageResult.getTotal());
        return page;
    }


}